/**
 *@Description
 *@author your name
 *@createDate 2011-7-6 上午06:49:52
 *@version V1.0
 */
package com.welldone.service.check;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.welldone.common.CommonData;
import com.welldone.service.DBService;
import com.welldone.service.offer.OfferManager;
import com.welldone.util.CommonUtil;
import com.welldone.util.DateUtils;

public class CheckManager {

	/**
	 * 结帐
	 *@author your name
	 *@createDate 2011-7-6
	 *@param map
	 *@return
	 */
	public boolean addCheck(Map map){
		boolean flag = false;
		String sql = "insert into t_check (checkId,tableId,total,checkType,isPrint) values(?,?,?,?,?,?)";
		String checkId = UUID.randomUUID().toString();
		Double total = Double.valueOf(map.get("total").toString());
		Double pocket = 0.0;
		Date d = new Date();
		boolean isVip = Boolean.getBoolean(map.get("isVip").toString());
		if(isVip){
			total = total*(CommonData.VIP_DISCOUNT/100);
		}
		Date date = new Date();
		List list = new OfferManager().getOfferByDate(DateUtils.formatDate12(date), DateUtils.formatDate12(date)); //查询当前的优惠，用于最后计算金额
		Map m = (Map)list.get(0);
		total = total*(Integer.parseInt(m.get("discount").toString())/100);
		
		Object[] args = {checkId,map.get("tableId"),total,map.get("checkType"),map.get("offerType"),map.get("isPrint")};
		int result = DBService.getJdbcTemplate().update(sql,args);
		if(result !=-1){
			flag = true;
		}
		return flag;
	}
	
	
	/**
	 * 根据条件查询结帐单
	 *@author your name
	 *@createDate 2011-7-8
	 *@param queryMap
	 *@return
	 */
	public List getCheckByCondition(Map queryMap){
		StringBuffer where = new StringBuffer();
		if(null!=queryMap){
			if(CommonUtil.equalsNotNull(queryMap.get("checkId"))){
				where.append(" and checkId = "+queryMap.get("checkId"));
			}
			
			if(CommonUtil.equalsNotNull(queryMap.get("tableId"))){
				where.append(" and tableId = "+queryMap.get("tableId"));
			}
			
			if(CommonUtil.equalsNotNull(queryMap.get("checkType"))){
				where.append(" and checkType = "+queryMap.get("checkType"));
			}

			if(CommonUtil.equalsNotNull(queryMap.get("offerType"))){
				where.append(" and offerType = "+queryMap.get("offerType"));
			}
			
		}
		String sql = "select * from t_check where 1=1 "+where.toString();
		return DBService.getJdbcTemplate().queryForList(sql);
	}
}


